<template>
  <van-button
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    :loading="followLoading"
    v-if="!isFollow"
    @click="cancelOraddFollow"
  >关注</van-button>
  <van-button
    class="follow-btn"
    round
    v-else
    size="small"
    :loading="followLoading"
    @click="cancelOraddFollow"
  >已关注</van-button>
</template>

<script>
import { sendCancelOraddFollow } from '@/api/articleList.js'

export default {
  data () {
    return {
      // 关注flag
      followLoading: false
    }
  },
  model: {
    prop: 'isFollow',
    event: 'upFollow'
  },
  props: {
    isFollow: {
      type: Boolean,
      required: true
    },
    followId: {
      type: [Object, String, Number],
      requried: true
    }
  },
  methods: {
    // 取消或添加关注
    async cancelOraddFollow () {
      console.log(this.isFollow)
      this.followLoading = true
      try {
        if (this.isFollow) { // 已关注, 取消关注
          await sendCancelOraddFollow(this.isFollow, this.followId)
        } else { // 未关注, 添加关注
          await sendCancelOraddFollow(this.isFollow, this.followId)
        }
        this.$emit('upFollow', !this.isFollow)
      } catch (e) {
        let message = '操作失败, 请稍后重试'
        if (e.response && e.response.status === 400) {
          message = '不能关注自己'
        }
        this.$toast(message)
      }
      this.followLoading = false
    }
  }
}
</script>

<style scoped>

</style>
